package com.lagou.edu.ad.remote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.edu.ad.entity.PromotionAd;
import com.lagou.edu.ad.entity.PromotionSpace;
import com.lagou.edu.ad.service.IPromotionAdService;
import com.lagou.edu.ad.service.IPromotionSpaceService;
import com.lagou.edu.dto.PromotionSpaceDTO;
import com.lagou.edu.dto.PromotionAdDTO;

import com.lagou.edu.remote.AdRemoteService;
import com.lagou.edu.response.ResponseDTO;
import com.lagou.edu.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ad")
public class AdRemoteServiceImpl implements AdRemoteService {
    /**
     * 有效状态
     */
    private static final Integer IS_USEFUL = 1;
    /**
     * 上架 - 1， 下架 - 0
     */
    private static final Integer ON_SHELF = 1;
    private static final Integer OFF_SHELF = 0;

    @Autowired
    private IPromotionSpaceService promotionSpaceService;
    @Autowired
    private IPromotionAdService promotionAdService;

    @GetMapping("/space/getAllSpaces")
    @Override
    public List<PromotionSpaceDTO> getAllSpaces() {
        List<PromotionSpace> promotionSpaceList = promotionSpaceService.list();
        return ConvertUtil.convertList(promotionSpaceList, PromotionSpaceDTO.class);
    }

    @GetMapping("/getAdBySpaceKey")
    @Override
    public List<PromotionSpaceDTO> getAdBySpaceKey(String[] spaceKey) {
        if (spaceKey == null || spaceKey.length <= 0) {
            return null;
        }

        List<PromotionSpaceDTO> promotionSpaceDTOList = new ArrayList<>();
        Date curTime = new Date();
        // 遍历
        for (String key : spaceKey) {
            QueryWrapper spaceQueryWrapper = new QueryWrapper();
            spaceQueryWrapper.eq("spaceKey", key);
            // 获取广告位
            PromotionSpace promotionSpace = promotionSpaceService.getOne(spaceQueryWrapper);
            if (promotionSpace == null){
                continue;
            }

            // 获取当前广告位对应的广告
            QueryWrapper adQueryWrapper = new QueryWrapper();
            adQueryWrapper.eq("spaceId", promotionSpace.getId());
            // 上架状态 并且 当前日期处于有效期内
            adQueryWrapper.eq("status", IS_USEFUL);
            adQueryWrapper.lt("startTime", curTime);
            adQueryWrapper.gt("endTime", curTime);
            // 查询
            List promotionAdList = promotionAdService.list(adQueryWrapper);
            List adDTOList = ConvertUtil.convertList(promotionAdList, PromotionAdDTO.class);

            // 转换成promotionSpaceDTO对象
            PromotionSpaceDTO promotionSpaceDTO = ConvertUtil.convert(promotionSpace, PromotionSpaceDTO.class);
            // 构造dto
            promotionSpaceDTO.setAdDTOList(adDTOList);
            promotionSpaceDTOList.add(promotionSpaceDTO);
        }
        return promotionSpaceDTOList;
    }


    @PostMapping("/space/saveOrUpdateSpace")
    @Override
    public ResponseDTO saveOrUpdateSpace(PromotionSpaceDTO spaceDTO) {
        ResponseDTO responseDTO = ResponseDTO.success();
        // DTO对象 转成 数据库对象
        PromotionSpace space = ConvertUtil.convert(spaceDTO, PromotionSpace.class);
        Date currentTime = new Date();

        space.setUpdateTime(currentTime);
        // 新建广告位
        if (space.getId() == null){
            space.setCreateTime(currentTime);
            space.setIsDel(0);
        }
        // 保存或者编辑广告位
        try{
            boolean success = promotionSpaceService.saveOrUpdate(space);
        }catch (Exception e){
            responseDTO = ResponseDTO.ofError(e.getMessage());
        }
        return responseDTO;
    }

    @GetMapping("/space/getSpaceById")
    @Override
    public PromotionSpaceDTO getSpaceById(Integer id) {
        PromotionSpace space = promotionSpaceService.getById(id);
        PromotionSpaceDTO spaceDTO = ConvertUtil.convert(space, PromotionSpaceDTO.class);
        return spaceDTO;
    }

    @GetMapping("/space/getAllAds")
    @Override
    public List<PromotionAdDTO> getAllAds() {
        List<PromotionAd> promotionAdList = promotionAdService.list();
        return ConvertUtil.convertList(promotionAdList, PromotionAdDTO.class);
    }

    @PostMapping("/saveOrUpdateAd")
    @Override
    public ResponseDTO saveOrUpdateAd(PromotionAdDTO adDTO) {
        ResponseDTO responseDTO = ResponseDTO.success();
        // DTO对象 转成 数据库对象
        PromotionAd ad = ConvertUtil.convert(adDTO, PromotionAd.class);
        Date currentTime = new Date();

        ad.setUpdateTime(currentTime);
        // 新建广告位
        if (ad.getId() == null){
            ad.setCreateTime(currentTime);
            ad.setStatus(ON_SHELF);
        }
        // 保存或者编辑广告位
        try{
            boolean success = promotionAdService.saveOrUpdate(ad);
        }catch (Exception e){
            responseDTO = ResponseDTO.ofError(e.getMessage());
        }
        return responseDTO;
    }

    @GetMapping("/getAdById")
    @Override
    public PromotionAdDTO getAdById(Integer id) {
        PromotionAd ad = promotionAdService.getById(id);
        PromotionAdDTO adDTO = ConvertUtil.convert(ad, PromotionAdDTO.class);
        return adDTO;
    }
}
